//------------------------------------------------------------------------------
// <copyright file="NameValidation.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.IO;
using System.Linq;
using System.Windows.Controls;

namespace DotBeer.UI.WPFClient
{
    public class NameValidation : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            string[] badChars = Path.GetInvalidFileNameChars().Where(c => !char.IsControl(c)).Select(c => c.ToString()).ToArray();
            
            foreach (string s in badChars)
            {
                if (value.ToString().Contains(s))
                {
                    string message = String.Format("Name cannot contain following chars: {0}", String.Join(" ", badChars));
                    return new ValidationResult(false, message);
                }
            }

            if (value.ToString().Trim().Length == 0)
                return new ValidationResult(false, "Name cannot be empty");

            return new ValidationResult(true, null);
        }
    }
}
